package cn.darkfire.web.Utils;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.junit.Test;

import javax.sql.DataSource;
import java.sql.Connection;
import java.util.Properties;

public class DruidUtils {
    private static DataSource ds = null;
    static {
        Properties pro = new Properties();
        pro.setProperty("driverClassName","com.mysql.jdbc.Driver");
        pro.setProperty("url","jdbc:mysql://127.0.0.1:3306/sy3");
        pro.setProperty("username","root");
        pro.setProperty("password","123456");
        pro.setProperty("initialSize","5");
        pro.setProperty("maxActive","10");
        pro.setProperty("maxWait","3000");
        try{
            ds = DruidDataSourceFactory.createDataSource(pro);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public static DataSource getDs(){
        return ds;
    }

    public static Connection getConnection(){
        try{
            return ds.getConnection();
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }

    }

    //单元测试
    @Test
    public void test(){
        for(int i = 0;i<=10;i++){
            Connection con = getConnection();
            System.out.println(i+":"+con);
            if(i==5){
                try{
                    con.close(); //归还连接池
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
    }
}
